package com.example.peibei.ui.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Order;
import com.dingtao.common.bean.RefreshResult;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.DelateBillPresenter;
import com.example.peibei.service.presenter.HomeOrderPresenter;
import com.example.peibei.service.presenter.ISendBillPresenter;
import com.example.peibei.ui.adapter.HomeOrderListAdapter;
import com.example.peibei.ui.adapter.MyBillListAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ISendBillActivity extends WDActivity {
    @BindView(R.id.order_list)
    EmptyRecyclerView order_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    List<Order.RecordsDTO> orders;
    private MyBillListAdapter homeOrderListAdapter;
    private ISendBillPresenter iSendBillPresenter;
    private SPUtils spUtils;
    private String token;
    private DelateBillPresenter delateBillPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_i_send_bill;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(ISendBillActivity.this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        orders = new ArrayList<>();
        order_list.setLayoutManager(new LinearLayoutManager(this));
        homeOrderListAdapter = new MyBillListAdapter(orders);
        order_list.setAdapter(homeOrderListAdapter);
        order_list.setEmptyView(empty_view);
        iSendBillPresenter = new ISendBillPresenter(new OrderCall());
        delateBillPresenter = new DelateBillPresenter(new DelateCall());
        homeOrderListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Order.RecordsDTO order = (Order.RecordsDTO) adapter.getData().get(position);
            String publishOrderId = order.getPublishOrderId();
            Bundle bundle = new Bundle();
            bundle.putString("publishOrderId", publishOrderId);
            bundle.putString("type", "0");
            intent(OrderDetailActivity.class, bundle);
        });
        homeOrderListAdapter.addChildClickViewIds(R.id.ll_delate, R.id.ll_edit);
        homeOrderListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                Order.RecordsDTO data = (Order.RecordsDTO) adapter.getData().get(position);
                if (view.getId() == R.id.ll_delate) {
                    showDelateDialog(data.getPublishOrderId());
                } else if (view.getId() == R.id.ll_edit) {
                    Bundle bundle = new Bundle();
                    bundle.putString("orderId", data.getPublishOrderId());
                    intent(EditOrderActivity.class, bundle);
                }
            }
        });
    }

    private void showDelateDialog(String orderId) {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(ISendBillActivity.this);
        final View dialogView = LayoutInflater.from(ISendBillActivity.this)
                .inflate(R.layout.dialog_bind_account, null);
        normalDialog.setView(dialogView);
        TextView tv_name = dialogView.findViewById(R.id.tv_name);
        TextView tv_title = dialogView.findViewById(R.id.tv_title);
        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_sure = dialogView.findViewById(R.id.tv_sure);
        tv_name.setText("提示");
        tv_title.setText("确定删除当前订单吗？");
        tv_sure.setText("删除");
        AlertDialog alertDialog = normalDialog.create();
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
                delateBillPresenter.reqeust("Bearer " + token, orderId);
            }
        });
        alertDialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        iSendBillPresenter.reqeust(true, "Bearer " + token);
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    class OrderCall implements DataCall<Order> {
        @Override
        public void success(Order data, Object... args) {
            //添加列表并刷新
            if (iSendBillPresenter.getPage() == 1) {
                orders.clear();
            }
            homeOrderListAdapter.setNewInstance(data.getRecords());
            homeOrderListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class DelateCall implements DataCall<String> {
        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("删除成功");
            iSendBillPresenter.reqeust(true, "Bearer " + token);
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    @Override
    protected void destoryData() {

    }
}
